//
//  YLDSyetemAuthorizationManager.m
//  Pods
//
//  Created by DonYau on 2017/7/25.
//
//

#import "YLDSyetemAuthorizationManager.h"
#import <UIKit/UIKit.h>
#import <AssetsLibrary/AssetsLibrary.h>
#import <Photos/PHPhotoLibrary.h>
#import <AVFoundation/AVFoundation.h>

@implementation YLDSyetemAuthorizationManager

+(void)authorizationStatusWithAuthorizationType:(YLDAuthorizationType)authorizationType withAuthorizationStatusBlock:(void (^)(YLDAuthorizationStatus))authStatusBlock{
    if (authorizationType == YLDAuthorizationTypePhotoLibrary) {
        [YLDSyetemAuthorizationManager getPhotoLibraryAuthorizationStatus:^(YLDAuthorizationStatus status) {
            authStatusBlock(status);
        }];
    }else if (authorizationType == YLDAuthorizationTypeCamera){
        [YLDSyetemAuthorizationManager getCameraAuthorizationStatus:^(YLDAuthorizationStatus status) {
            authStatusBlock(status);
        }];
    }
}


#pragma mark - 相册
+ (void)getPhotoLibraryAuthorizationStatus:(void (^)(YLDAuthorizationStatus))authStatusBlock{
    YLDAuthorizationStatus authStatus = YLDAuthorizationStatusNotDetermined;
    CGFloat systemVersion = [[UIDevice currentDevice].systemVersion floatValue];
    if (systemVersion < 8) {
        ALAuthorizationStatus assetsLibraryauthStatus = [ALAssetsLibrary authorizationStatus];
        switch (assetsLibraryauthStatus) {
            case ALAuthorizationStatusRestricted:
                authStatus = YLDAuthorizationStatusRestricted;
                break;
            case ALAuthorizationStatusDenied:
                authStatus = YLDAuthorizationStatusDenied;
                break;
            case ALAuthorizationStatusAuthorized:
                authStatus = YLDAuthorizationStatusAuthorized;
                break;
            default:
                authStatus = YLDAuthorizationStatusNotDetermined;
                break;
        }
    }else{
        PHAuthorizationStatus photoLibraryAuthStatus = [PHPhotoLibrary authorizationStatus];
        switch (photoLibraryAuthStatus) {
            case PHAuthorizationStatusRestricted:
                authStatus = YLDAuthorizationStatusRestricted;
                break;
            case PHAuthorizationStatusDenied:
                authStatus = YLDAuthorizationStatusDenied;
                break;
            case PHAuthorizationStatusAuthorized:
                authStatus = YLDAuthorizationStatusAuthorized;
                break;
            default:
                authStatus = YLDAuthorizationStatusNotDetermined;
                break;
        }
    }
    
    if (authStatus == YLDAuthorizationStatusNotDetermined){
        [PHPhotoLibrary requestAuthorization:^(PHAuthorizationStatus status) {
            dispatch_async(dispatch_get_main_queue(), ^{
                switch (status) {
                    case PHAuthorizationStatusRestricted:
                        authStatusBlock(YLDAuthorizationStatusRestricted);
                        break;
                    case PHAuthorizationStatusDenied:
                        authStatusBlock(YLDAuthorizationStatusDenied);
                        break;
                    case PHAuthorizationStatusAuthorized:
                        authStatusBlock(YLDAuthorizationStatusAuthorized);
                        break;
                    default:
                        authStatusBlock(YLDAuthorizationStatusNotDetermined);
                        break;
                }
            });
        }];
    }else{
        authStatusBlock(authStatus);
    }
}

#pragma mark - 相机
+ (void)getCameraAuthorizationStatus:(void (^)(YLDAuthorizationStatus))authStatusBlock{
    AVAuthorizationStatus authStatus = [AVCaptureDevice authorizationStatusForMediaType:AVMediaTypeVideo];
    if (authStatus == AVAuthorizationStatusNotDetermined) {
        [AVCaptureDevice requestAccessForMediaType:AVMediaTypeVideo completionHandler:^(BOOL granted) {
            dispatch_async(dispatch_get_main_queue(), ^{
                if (granted) {
                    authStatusBlock(YLDAuthorizationStatusAuthorized);
                } else {
                    authStatusBlock(YLDAuthorizationStatusDenied);
                }
            });
        }];
    }else if (authStatus == AVAuthorizationStatusAuthorized) {
        authStatusBlock(YLDAuthorizationStatusAuthorized);
    } else if (authStatus == AVAuthorizationStatusDenied) {
        authStatusBlock(YLDAuthorizationStatusDenied);
    } else if (authStatus == AVAuthorizationStatusRestricted) {
        authStatusBlock(YLDAuthorizationStatusRestricted);
    }
}


@end
